package cn.sofwin.web.blog.dao;

import cn.sofwin.core.dao.BaseDao;
import cn.sofwin.core.utils.JdbcUtils;
import cn.sofwin.web.blog.entity.Admin;

import java.util.List;
/**
 * 管理员数据访问层
 * @author ZX
 * @version 1.0
 */
public class AdminDao extends BaseDao<Admin> {
    @Override
    public int insert(Admin admin) {
        String sql="insert into admin(username,password,level) value (?,?,?)";
        return JdbcUtils.insert(sql,admin.getUsername(),admin.getPassword(),admin.getLevel());
    }

    @Override
    public int deleteById(Integer id) {
        String sql="delete from admin where id=?";
        return JdbcUtils.update(sql,id);
    }

    @Override
    public int update(Admin admin) {
        String sql="update admin set password=?,level=? where id=?";
        return JdbcUtils.update(sql,admin.getPassword(),admin.getLevel(),admin.getId());
    }

    @Override
    public Admin get(Integer id) {
        String  sql="select * from admin where id=?";
        return JdbcUtils.queryForObject(Admin.class,sql,id);
    }

    @Override
    public List<Admin> all() {
        String  sql="select * from admin ";
        return JdbcUtils.queryForList(Admin.class,sql);
    }

    public Admin findByName(String name) {
        String sql ="select * from admin where username=?";
        return JdbcUtils.queryForObject(Admin.class,sql,name);
    }
}
